window.onload = function () {
    $('.loading-shadow').addClass('mdui-hidden');
}
$(function () {
    let $groupFab = $('#groupFab');
    let $newsFab = $('#newsFab');
    let $prosFab = $('#prosFab');
    let $indexFab = $('#indexFab');
    $('.mdui-ripple-white').click(function () {
        let now_href = $(this).attr('href');
        switch (now_href) {
            case '#tab1':
                $groupFab.addClass('mdui-fab-hide');
                $newsFab.addClass('mdui-fab-hide');
                $prosFab.addClass('mdui-fab-hide');
                $indexFab.removeClass('mdui-fab-hide');
                break;
            case '#tab2':
                $groupFab.addClass('mdui-fab-hide');
                $newsFab.addClass('mdui-fab-hide');
                $prosFab.removeClass('mdui-fab-hide');
                $indexFab.addClass('mdui-fab-hide');
                break;
            case '#tab3':
                $groupFab.removeClass('mdui-fab-hide');
                $newsFab.addClass('mdui-fab-hide');
                $prosFab.addClass('mdui-fab-hide');
                $indexFab.addClass('mdui-fab-hide');
                break;
            case '#tab4':
                $groupFab.addClass('mdui-fab-hide');
                $newsFab.removeClass('mdui-fab-hide');
                $prosFab.addClass('mdui-fab-hide');
                $indexFab.addClass('mdui-fab-hide');
                break;
            case '#tab5':
                $groupFab.addClass('mdui-fab-hide');
                $newsFab.addClass('mdui-fab-hide');
                $prosFab.addClass('mdui-fab-hide');
                $indexFab.addClass('mdui-fab-hide');
                break;
            default:
        }
    })
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $('#edit-wx').click(function () {
        $('#upload-wx').click();
    })
    $('#upload-wx').change(function () {
        $('#wxForm').ajaxSubmit({
            dataType: "json", //数据格式为json
            clearForm: true,   //boolean，默认为false，成功提交后是否清除所有表单元素的值
            resetForm: true,  //boolean，默认为false，成功提交后是否重置所有表单元素的值
            timeout: 3000,    //number，单位ms，限制请求的时间，当请求大于设置的时间后，跳出请求
            beforeSend: function () { //开始上传
                $('#wx-progress').removeClass('mdui-hidden');
            },
            uploadProgress: function (event, position, total, percentComplete) {
                let percentVal = percentComplete + "%"; //获得进度
                $('#wx-progress .mdui-progress-determinate').width(percentVal);
            },
            success: function (data) {
                console.log(data);
                if (data['status'] === 0) {
                    let image = 'http://' + window.location.host + '/storage/uploads/' + data['filename'];
                    $('#com-wx').attr('src', image);
                    $('#submit-wx').val(image);
                    mdui.snackbar({
                        message: '上传成功'
                    });
                } else {
                    console.log('0');
                }
                $('#wx-progress').addClass('mdui-hidden');
            },
            error: function (xhr, tS, rT) { //上传失败
                console.log(xhr);
                $('#wx-progress').addClass('mdui-hidden');
            }
        });
        return false;
    })
    $('#submit-tab5').click(function () {
        let com_name = $('#com-name').val();
        let com_dede = $('#com-dede').val();
        let com_tel = $('#com-tel').val();
        let com_fax = $('#com-fax').val();
        let com_add = $('#com-add').val();
        let com_wx = $('#submit-wx').val();
        let com_ipc = $('#com-ipc').val();
        let com_cpr = $('#com-cpr').val();
        let com_rtt = $('#com-rtt').val();
        let com_logo = $('#com-logo').val();
        $.ajax({
            url: 'http://' + window.location.host + '/update/ao',
            type: 'post',
            async: true,
            timeOut: 30000,
            dataType: 'json',
            data: {
                'com_name': com_name,
                'com_dede': com_dede,
                'com_tel': com_tel,
                'com_fax': com_fax,
                'com_add': com_add,
                'com_wx': com_wx,
                'com_ipc': com_ipc,
                'com_cpr': com_cpr,
                'com_rtt': com_rtt,
                'com_logo': com_logo
            },
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('input').attr("disabled", true);
                $('textarea').attr("disabled", true);
                $('#submit-progress').removeClass("mdui-hidden");
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('input').attr("disabled", false);
                $('textarea').attr("disabled", false);
                $('#submit-progress').addClass("mdui-hidden");
                if (data['status'] === 0) {
                    mdui.snackbar({
                        message: '更新成功'
                    });
                } else {
                    mdui.snackbar({
                        message: '更新失败'
                    });
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('input').attr("disabled", false);
                $('textarea').attr("disabled", false);
                $('#submit-progress').addClass("mdui-hidden");
            },
        });
        return false;
    });
    let $group_panel = $('#group_panel');
    $('#group_add').click(function () {
        let chip_count = $group_panel.find('.mdui-panel-item').length;
        chip_count = chip_count - 1;
        let last_id = $group_panel.find('.mdui-panel-item').eq(chip_count).data('id');
        console.log(last_id);
        let chip_id = last_id + 1;
        let new_chip = '<div class="mdui-panel-item" data-id="' + chip_id + '"><div class="mdui-panel-item-header"><div class="mdui-panel-item-title">' + chip_id + '</div><div class="mdui-panel-item-summary summary-name"></div><div class="mdui-panel-item-summary summary-job"></div><i class="mdui-panel-item-arrow mdui-icon material-icons">keyboard_arrow_down</i></div><div class="mdui-panel-item-body"><div class="mdui-container"><form class="usForm"><div class="mdui-textfield mdui-textfield-floating-label"><label class="mdui-textfield-label">成员姓名</label><input name="us-name" class="mdui-textfield-input" type="text" value=""/></div><div class="mdui-textfield mdui-textfield-floating-label"><label class="mdui-textfield-label">成员职位</label><input name="us-job" class="mdui-textfield-input" type="text" value=""/></div><div class="mdui-card" style="width: 200px;height: 250px;"><div class="mdui-card-media"><img class="mdui-img-circle us-avatar-img"src="http://jiaju.local.com/storage/uploads/about-team-01.png" style="width: 200px; height: 200px"/><div class="mdui-card-media-covered"><div class="mdui-card-primary"><div class="mdui-card-primary-title">成员头像</div></div></div></div><div class="mdui-progress mdui-hidden avatar-progress"><div class="mdui-progress-determinate" style="width: 0;"></div></div><div class="mdui-card-actions"><button class="mdui-btn mdui-ripple edit-avatar">修改</button><span class="mdui-text-color-black-disabled"><small>建议200x200</small></span></div><input class="mdui-hidden submit-avatar" name="us-avatar" value="about-team-01.png"/></div></form><form class="avatarForm" data-id="' + chip_id + '" enctype="multipart/form-data" action="http://jiaju.local.com/update/avatar" method="POST"><input type="file" name="upload-avatar" class="mdui-hidden upload-avatar" accept="image/png,image/jpeg"/></form></div><div class="mdui-panel-item-actions"><div class="mdui-progress mdui-hidden submit-progress"><div class="mdui-progress-indeterminate"></div></div><button class="mdui-btn mdui-ripple" mdui-panel-item-close>cancel</button><button class="mdui-btn mdui-ripple mdui-color-pink-a200 us-save">save</button></div></div></div>';
        $group_panel.html($group_panel.html() + new_chip);
        return false;
    })
    $group_panel.on('click', '.us-save', function () {
        let $panel = $(this).parents('.mdui-panel-item');
        let id = $panel.data('id');
        // 保存
        $.ajax({
            url: 'http://' + window.location.host + '/update/us/' + id,
            type: 'post',
            async: true,
            timeOut: 30000,
            dataType: 'json',
            data: $panel.find('.usForm').serialize(),
            beforeSend: function () {
                //函数体，数据发送前执行。
                $panel.find('input').attr("disabled", true);
                $panel.find('button').attr("disabled", true);
                $panel.find('.submit-progress').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $panel.find('input').attr("disabled", false);
                $panel.find('button').attr("disabled", false);
                $panel.find('.submit-progress').addClass('mdui-hidden');
                $panel.find('.summary-name').html(data['data']['name']);
                $panel.find('.summary-job').html(data['data']['job']);
                if (data['status'] === 0) {
                    mdui.snackbar({
                        message: '更新成功'
                    });
                } else if (data['status'] === 1) {
                    $panel.find('.mdui-panel-item-actions').html('<button class="mdui-btn mdui-ripple mdui-color-black us-delete" mdui-dialog="{target: \'#delete-us-' + id + '\'}">delete</button><button class="mdui-btn mdui-ripple" mdui-panel-item-close>cancel</button><button class="mdui-btn mdui-ripple mdui-color-pink-a200 us-save">save</button><div class="mdui-dialog" id="delete-us-' + id + '"><div class="mdui-dialog-title">确定删除？</div><div class="mdui-dialog-content">删除后将无法还原！</div><div class="mdui-dialog-actions"><button class="mdui-btn mdui-ripple" mdui-dialog-close>cancel</button><button class="mdui-btn mdui-ripple mdui-color-black delete-erase" mdui-dialog-confirm>erase</button></div></div>');
                    mdui.snackbar({
                        message: '添加成功'
                    });
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $panel.find('input').attr("disabled", false);
                $panel.find('button').attr("disabled", false);
                $panel.find('.submit-progress').addClass('mdui-hidden');
            },
        });
        return false;
    })
    $group_panel.on('click', '.delete-erase', function () {
        let $panel = $(this).parents('.mdui-panel-item');
        let id = $panel.data('id');
        // 删除
        $.ajax({
            url: 'http://' + window.location.host + '/delete/us/' + id,
            type: 'post',
            async: true,
            timeOut: 30000,
            dataType: 'json',
            data: $panel.find('.usForm').serialize(),
            beforeSend: function () {
                //函数体，数据发送前执行。
                $panel.find('input').attr("disabled", true);
                $panel.find('button').attr("disabled", true);
                $panel.find('.submit-progress').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $panel.find('input').attr("disabled", false);
                $panel.find('button').attr("disabled", false);
                $panel.find('.submit-progress').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    $panel.remove();
                    mdui.snackbar({
                        message: '删除成功'
                    });
                } else {
                    mdui.snackbar({
                        message: '删除失败'
                    });
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $panel.find('input').attr("disabled", false);
                $panel.find('button').attr("disabled", false);
                $panel.find('.submit-progress').addClass('mdui-hidden');
            },
        });
        return false;
    })
    $group_panel.on('click', '.edit-avatar', function () {
        let $panel = $(this).parents('.mdui-panel-item');
        $panel.find('.upload-avatar').click();
        return false;
    })
    $group_panel.on('change', '.upload-avatar', function () {
        let $panel = $(this).parents('.mdui-panel-item');
        let $form = $panel.find('.avatarForm');
        $form.ajaxSubmit({
            dataType: "json", //数据格式为json
            clearForm: true,   //boolean，默认为false，成功提交后是否清除所有表单元素的值
            resetForm: true,  //boolean，默认为false，成功提交后是否重置所有表单元素的值
            timeout: 3000,    //number，单位ms，限制请求的时间，当请求大于设置的时间后，跳出请求
            beforeSend: function () { //开始上传
                $panel.find('.avatar-progress').removeClass('mdui-hidden');
            },
            uploadProgress: function (event, position, total, percentComplete) {
                let percentVal = percentComplete + "%"; //获得进度
                $panel.find('.avatar-progress .mdui-progress-determinate').width(percentVal);
            },
            success: function (data) {
                console.log(data);
                if (data['status'] === 1) {
                    let image = 'http://' + window.location.host + '/storage/uploads/' + data['filename'];
                    $panel.find('.us-avatar-img').attr('src', image);
                    $panel.find('.submit-avatar').val(data['filename']);
                    mdui.snackbar({
                        message: '上传成功'
                    });
                } else {
                    console.log('0');
                }
                $panel.find('.avatar-progress').addClass('mdui-hidden');
            },
            error: function (xhr, tS, rT) { //上传失败
                console.log(xhr);
                $panel.find('.avatar-progress').addClass('mdui-hidden');
            }
        });
        return false;
    })
    // news
    let $news_table = $('#news-table');
    $newsFab.click(function () {
        if ($news_table.find('.mdui-table-row-selected').length === 1) {
            $('.news-edit-fab').removeClass('mdui-hidden');
            $('.news-delete-fab').removeClass('mdui-hidden');
        } else if ($news_table.find('.mdui-table-row-selected').length === 0) {
            $('.news-delete-fab').addClass('mdui-hidden');
            $('.news-edit-fab').addClass('mdui-hidden');
        } else {
            $('.news-edit-fab').addClass('mdui-hidden');
            $('.news-delete-fab').removeClass('mdui-hidden');
        }
    })
    // 删除news
    $('#delete-news-erase').on('click', function () {
        let id_list = [];
        for (let i = 0; i < $news_table.find('.mdui-table-row-selected').length; i++) {
            id_list.push($news_table.find('.mdui-table-row-selected').eq(i).data('id'));
        }
        $.ajax({
            url: 'http://' + window.location.host + '/delete/news',
            type: 'post',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: {
                'id_list': id_list
            },
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    $('.news-delete-fab').addClass('mdui-hidden');
                    $('.news-edit-fab').addClass('mdui-hidden');
                    mdui.snackbar({
                        message: '删除成功'
                    });
                    $news_table.find('.mdui-table-row-selected').remove();
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
        return false;
    })
    // 添加news
    $('.news-add-fab').on('click', function () {
        $('#news-type').val('add');
        $('#update-news-button').html('添加');
        $('#update-news-title').html('添加新闻');
        let last_id = $news_table.find('.news-tr').eq(0).data('id');
        console.log(last_id);
        let news_id = last_id + 1;
        $('#new-id').val(news_id);
    })
    // 编辑news
    $('.news-edit-fab').on('click', function () {
        $('#news-type').val('edit');
        $('#update-news-button').html('更新');
        $('#update-news-title').html('更新新闻');
        let $select = $news_table.find('.mdui-table-row-selected');
        let new_id = $news_table.find('.mdui-table-row-selected').data('id');
        $.ajax({
            url: 'http://' + window.location.host + '/get/new/' + new_id,
            type: 'get',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: {}, // $('form').serialize()
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                if (data['status'] === 0) {
                    $('#new-id').val(data['data'][0]['id']);
                    $('#new-title').val(data['data'][0]['title']);
                    $('#new-show').val(data['data'][0]['show']);
                    $('#new-img').attr('src', 'http://' + window.location.host + '/storage/uploads/' + data['data'][0]['show']);
                    $('#new-micro').val(data['data'][0]['micro']);
                    tinyMCE.editors[0].setContent(data['data'][0]['content']);
                }
                $('.loading-shadow').addClass('mdui-hidden');
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
    })
    // 返回news
    $('#news-back').on('click', function () {
        if ($('#news-type').val() === 'edit') {
            $('#new-id').val('');
            $('#new-title').val('');
            $('#new-img').attr('src', '');
            $('#new-micro').val('');
            tinyMCE.editors[0].setContent('');
        }
    })
    // 添加news的展示图
    $('#edit-show').on('click', function () {
        $('#upload-new-show-img').click();
    })
    $('#upload-new-show-img').change(function () {
        $('#newShowForm').ajaxSubmit({
            dataType: "json", //数据格式为json
            clearForm: true,   //boolean，默认为false，成功提交后是否清除所有表单元素的值
            resetForm: true,  //boolean，默认为false，成功提交后是否重置所有表单元素的值
            timeout: 3000,    //number，单位ms，限制请求的时间，当请求大于设置的时间后，跳出请求
            beforeSend: function () { //开始上传
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            uploadProgress: function (event, position, total, percentComplete) {
                let percentVal = percentComplete + "%"; //获得进度
            },
            success: function (data) {
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    let image = 'http://' + window.location.host + '/storage/uploads/' + data['filename'];
                    $('#new-img').attr('src', image);
                    $('#new-show').val(data['filename']);
                    mdui.snackbar({
                        message: '上传成功'
                    });
                }
            },
            error: function (xhr, tS, rT) { //上传失败
                console.log(xhr);
                $('.loading-shadow').addClass('mdui-hidden');
            }
        });
        return false;
    })
    // news插入图片
    $('#new-add-img').on('click', function () {
        $('#upload-new-content-img').click();
    })
    $('#upload-new-content-img').change(function () {
        $('#newContentForm').ajaxSubmit({
            dataType: "json", //数据格式为json
            clearForm: true,   //boolean，默认为false，成功提交后是否清除所有表单元素的值
            resetForm: true,  //boolean，默认为false，成功提交后是否重置所有表单元素的值
            timeout: 3000,    //number，单位ms，限制请求的时间，当请求大于设置的时间后，跳出请求
            beforeSend: function () { //开始上传
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            uploadProgress: function (event, position, total, percentComplete) {
                let percentVal = percentComplete + "%"; //获得进度
            },
            success: function (data) {
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    let image = 'http://' + window.location.host + '/storage/uploads/' + data['filename'];
                    tinyMCE.execCommand("mceInsertContent", false, "[img]" + image + "[/img]");
                    mdui.snackbar({
                        message: '上传成功'
                    });
                }
            },
            error: function (xhr, tS, rT) { //上传失败
                console.log(xhr);
                $('.loading-shadow').addClass('mdui-hidden');
            }
        });
        return false;
    });
    // 添加news上传和更新
    $('#update-news-button').on('click', function () {
        let content = tinyMCE.activeEditor.getContent();
        $('#new-content').val(content
            .replace(/(<!DOCTYPE\shtml>\n<html>\n<head>\n<\/head>\n<body>)/g, "<div class=\"new-details-container-t\">")
            .replace(/(<\/body>\n<\/html>)/g, "</div>")
            .replace(/(\[img\])/g, "<img class=\"new-details-container-t-img\" src=\"")
            .replace(/(\[\/img\])/g, "\">")
            .replace(/(\n)/g, "</div><div class=\"new-details-container-t\">")
        );
        $.ajax({
            url: 'http://' + window.location.host + '/update/news',
            type: 'post',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: $('#newForm').serialize(),
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                $('new-id').val('');
                $('new-title').val('');
                $('new-micro').val('');
                $('new-image').val('');
                $('new-show').val('');
                $('new-img').attr('img', '');
                $('new-content').val('');
                tinyMCE.editors[0].setContent('');
                if (data['status'] === 1) {
                    let $news_tr = $(".news-tr[data-id='" + data['data']['id'] + "']");
                    $news_tr.find('.origin-title').html(data['data']['title']);
                    $news_tr.find('.origin-micro').html(data['data']['micro']);
                    $news_tr.find('.origin-show').html(data['data']['show']);
                    $news_tr.find('.origin-content').html(data['data']['content'].replace(/<\/?.+?>/g, ""));
                    $news_tr.find('.origin-show-2').html('http://' + window.location.host + '/storage/uploads/' + data['data']['show']);
                    mdui.snackbar({
                        message: '修改成功'
                    });
                } else if (data['status'] === 0) {
                    mdui.snackbar({
                        message: '添加成功'
                    });
                } else {
                    mdui.snackbar({
                        message: 'fail'
                    });
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
    })
    // pros
    let $pros_table = $('#pros-table');
    $prosFab.click(function () {
        if ($pros_table.find('.mdui-table-row-selected').length === 1) {
            $('.pros-edit-fab').removeClass('mdui-hidden');
            $('.pros-delete-fab').removeClass('mdui-hidden');
        } else if ($pros_table.find('.mdui-table-row-selected').length === 0) {
            $('.pros-delete-fab').addClass('mdui-hidden');
            $('.pros-edit-fab').addClass('mdui-hidden');
        } else {
            $('.pros-edit-fab').addClass('mdui-hidden');
            $('.pros-delete-fab').removeClass('mdui-hidden');
        }
    })
    // 删除pros
    $('#delete-pros-erase').on('click', function () {
        let id_list = [];
        for (let i = 0; i < $pros_table.find('.mdui-table-row-selected').length; i++) {
            id_list.push($pros_table.find('.mdui-table-row-selected').eq(i).data('id'));
        }
        console.log(id_list);
        $.ajax({
            url: 'http://' + window.location.host + '/delete/pros',
            type: 'post',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: {
                'id_list': id_list
            },
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    $('.pros-delete-fab').addClass('mdui-hidden');
                    $('.pros-edit-fab').addClass('mdui-hidden');
                    $pros_table.find('.mdui-table-row-selected').remove();
                    mdui.snackbar({
                        message: '删除成功'
                    });
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
        return false;
    })
    // 添加pro
    $('.pros-add-fab').on('click', function () {
        $('#pros-type').val('add');
        $('#update-pros-button').html('添加');
        $('#update-pros-title').html('添加产品');
        let last_id = $pros_table.find('.pros-tr').eq(0).data('id');
        console.log(last_id);
        let pros_id = last_id + 1;
        $('#pro-id').val(pros_id);
    })
    // 修改pro
    $('.pros-edit-fab').on('click', function () {
        $('#update-pros-title').html('更新产品');
        $('#update-pors-button').html('更新');
        let pro_id = $pros_table.find('.mdui-table-row-selected').data('id');
        $.ajax({
            url: 'http://' + window.location.host + '/get/pro/' + pro_id,
            type: 'get',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').addClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                console.log(data.data[0].id);
                $('#pro-id').val(data.data[0].id);
                $('#pro-title').val(data.data[0].title);
                $('#pro-show-img').val(data.data[0].show_img);
                $('#pro-img').attr('src', 'http://' + window.location.host + '/storage/uploads/' + data.data[0].show_img);
                $('#pro-type').val(data.data[0].type);
                $('#pro-material').val(data.data[0].material);
                $('#pro-size').val(data.data[0].size);
                $('#pro-color').val(data.data[0].color);
                $('#pro-bag-type').val(data.data[0].bag_type);
                $('#pro-de-de').val(data.data[0].de_de);
                $('#pro-zh-type').val(data.data[0].zh_type);
                $('#pro-item-id').val(data.data[0].item_id);
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
    })
    $('#update-pors-button').on('click', function () {
        let pro_id = $('#pro-id').val();
        let $proFrom = $('#proForm');
        console.log($proFrom.serialize());
        $.ajax({
            url: 'http://' + window.location.host + '/update/pro',
            type: 'post',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: $proFrom.serialize(),
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').addClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                if (data['status'] === 0) {
                    let $origin = $('#pros-table').find('tbody').html();
                    let insert = '<tr class="pros-tr" data-id="' + data['data'][0]['id'] + '"><td class="mdui-table-cell-checkbox"><label class="mdui-checkbox"><input type="checkbox"><i class="mdui-checkbox-icon"></i></label></td><td class="mdui-text-truncate origin-title" style="max-width: 200px;">' + data['data'][0]['title'] + '</td><td><img class="mdui-img-rounded origin-show" src="http://' + window.location.host + '/storage/uploads/' + data['data'][0]['show_img'] + '" width="50px"/></td><td class="mdui-text-truncate origin-content" style="max-width: 200px;">' + data['data'][0]['type'] + '|' + data['data'][0]['material'] + '...' + '</td><td class="mdui-text-truncate origin-de-de" style="max-width: 200px;">' + data['data'][0]['de_de'] + '</td><td class="mdui-text-truncate origin-item-id" style="max-width: 200px;">' + data['data'][0]['item_id'] + '</td><td class="mdui-text-truncate" style="max-width: 200px;">' + data['data'][0]['created_at'] + '</td><td>' + data['data'][0]['created_id'] + '</td></tr>';
                    $('#pros-table').find('tbody').html(insert + $origin);
                    $('.loading-shadow').addClass('mdui-hidden');
                    mdui.snackbar({
                        message: '添加成功'
                    });
                    $('#pro-id').val('');
                    $('#pro-title').val('');
                    $('#pro-show-img').val('');
                    $('#pro-img').attr('src', '');
                    $('#pro-type').val('');
                    $('#pro-material').val('');
                    $('#pro-size').val('');
                    $('#pro-color').val('');
                    $('#pro-bag-type').val('');
                    $('#pro-de-de').val('');
                    $('#pro-zh-type').val('');
                    $('#pro-item-id').val('');
                } else if (data['status'] === 1) {
                    let $pros_tr = $(".pros-tr[data-id='" + data['data'][0]['id'] + "']");
                    console.log($pros_tr.data('id'));
                    $pros_tr.children('.origin-title').html(data['data'][0]['title']);
                    $pros_tr.find('.origin-content').html(data['data'][0]['content']);
                    $pros_tr.find('.origin-de-de').html(data['data'][0]['de_de']);
                    $pros_tr.find('.origin-item-id').html(data['data'][0]['item_id']);
                    $pros_tr.find('.origin-show').attr('src', 'http://' + window.location.host + '/storage/uploads/' + data['data'][0]['show_img']);
                    mdui.snackbar({
                        message: '更新成功'
                    });
                    $('#pro-id').val('');
                    $('#pro-title').val('');
                    $('#pro-show-img').val('');
                    $('#pro-img').attr('src', '');
                    $('#pro-type').val('');
                    $('#pro-material').val('');
                    $('#pro-size').val('');
                    $('#pro-color').val('');
                    $('#pro-bag-type').val('');
                    $('#pro-de-de').val('');
                    $('#pro-zh-type').val('');
                    $('#pro-item-id').val('');
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
    })
    // pro点击返回键删除所有值
    $('#pros-back').on('click', function () {
        $('#pro-id').val('');
        $('#pro-title').val('');
        $('#pro-show-img').val('');
        $('#pro-img').attr('src', '');
        $('#pro-type').val('');
        $('#pro-material').val('');
        $('#pro-size').val('');
        $('#pro-color').val('');
        $('#pro-bag-type').val('');
        $('#pro-de-de').val('');
        $('#pro-zh-type').val('');
        $('#pro-item-id').val('');
    })
    // 添加pro图片
    $('#edit-pro-show').on('click', function () {
        $('#upload-pro-show-img').click();
    })
    $('#upload-pro-show-img').change(function () {
        let $form = $('#proShowForm');
        $form.ajaxSubmit({
            dataType: "json", //数据格式为json
            clearForm: true,   //boolean，默认为false，成功提交后是否清除所有表单元素的值
            resetForm: true,  //boolean，默认为false，成功提交后是否重置所有表单元素的值
            timeout: 3000,    //number，单位ms，限制请求的时间，当请求大于设置的时间后，跳出请求
            beforeSend: function () { //开始上传
                $('#pro-pro').removeClass('mdui-hidden');
            },
            uploadProgress: function (event, position, total, percentComplete) {
                let percentVal = percentComplete + "%"; //获得进度
                $('#pro-progress').width(percentVal);
            },
            success: function (data) {
                console.log(data);
                if (data['status'] === 0) {
                    let image = 'http://' + window.location.host + '/storage/uploads/' + data['filename'];
                    $('#pro-img').attr('src', image);
                    $('#pro-show-img').val(data['filename']);
                    mdui.snackbar({
                        message: '上传成功'
                    });
                } else {
                    console.log('0');
                }
                $('#pro-pro').addClass('mdui-hidden');
            },
            error: function (xhr, tS, rT) { //上传失败
                console.log(xhr);
                $('#pro-pro').addClass('mdui-hidden');
            }
        });
        return false;
    })
    // 更新swiper
    let $swiper_panel = $('#swiper_panel');
    $('.swiper-save').on('click', function () {
        let $panel = $(this).parents('.mdui-panel-item');
        $.ajax({
            url: 'http://' + window.location.host + '/update/swiper',
            type: 'post',
            async: true,
            timeOut: 50000,
            dataType: 'json',
            data: $panel.find('.swiperForm').serialize(),
            beforeSend: function () {
                //函数体，数据发送前执行。
                $('.loading-shadow').removeClass('mdui-hidden');
            },
            success: function (data, textStatus, jqHXR) {
                //函数体，数据发送成功回调
                console.log(data);
                $('.loading-shadow').addClass('mdui-hidden');
                if (data['status'] === 0) {
                    // add
                } else if (data['status'] === 1) {
                    // update
                }
            },
            error: function (xhr, textStatus) {
                //函数体，数据发送失败回调
                console.log(xhr + textStatus);
                $('.loading-shadow').addClass('mdui-hidden');
            },
        });
    })

    $('.swiper-add-fab').on('click', function () {
        let chip_count = $swiper_panel.find('.mdui-panel-item').length;
        chip_count = chip_count - 1;
        let last_id = $swiper_panel.find('.mdui-panel-item').eq(chip_count).data('id');
        console.log(last_id);
        let chip_id = last_id + 1;
        let new_chip = '<div class="mdui-panel-item" data-id="' + chip_id + '"><div class="mdui-panel-item-header"><div class="mdui-panel-item-title">' + chip_id + '</div><div class="mdui-panel-item-summary summary-name"></div><div class="mdui-panel-item-summary summary-job"></div><i class="mdui-panel-item-arrow mdui-icon material-icons">keyboard_arrow_down</i></div><div class="mdui-panel-item-body"><div class="mdui-container"><form class="swiperForm" onsubmit="return false;"><div class="mdui-textfield mdui-textfield-floating-label"><label class="mdui-textfield-label">图片标题</label><input name="swiper-alt" class="mdui-textfield-input" type="text" value="" placeholder/></div><div class="mdui-textfield mdui-textfield-floating-label"><label class="mdui-textfield-label">图片链接</label><input name="swiper-go" class="mdui-textfield-input" type="text" value="" placeholder/><div class="mdui-textfield-helper">不用包括域名，如：index</div></div><div class="mdui-card" style="width: 200px"><div class="mdui-card-media"><div class="" style="width: 200px; height: 150px;"><img alt="" class="mdui-img-circle us-avatar-img" src="" style="width: auto; height: auto;max-width: 200px; max-height: 150px"/></div><div class="mdui-card-media-covered"><div class="mdui-card-primary"><div class="mdui-card-primary-title">展示图片</div></div></div></div><div class="mdui-progress mdui-hidden avatar-progress"><div class="mdui-progress-determinate" style="width: 0;"></div></div><div class="mdui-card-actions"><button class="mdui-btn mdui-ripple edit-avatar">修改</button><span class="mdui-text-color-black-disabled"><small>建议200x200</small></span></div><input type="hidden" name="swiper-filename" value=""/><input type="hidden" name="swiper-id" value="' + chip_id + '"/></div></form><form class="swiperShowForm" data-id="' + chip_id + '" enctype="multipart/form-data" action="http://jiaju.local.com/upload/swiper" method="POST"><input type="file" name="upload_swiper" class="mdui-hidden upload_swiper" accept="image/png,image/jpeg"/></form></div><div class="mdui-panel-item-actions"><div class="mdui-progress mdui-hidden swiper-submit-progress"><div class="mdui-progress-indeterminate"></div></div><button class="mdui-btn mdui-ripple mdui-color-black swiper-delete" mdui-dialog="{target: \'#delete-swiper-' + chip_id + '\'}">delete</button><button class="mdui-btn mdui-ripple" mdui-panel-item-close>cancel</button><button class="mdui-btn mdui-ripple mdui-color-pink-a200 swiper-save">save</button><div class="mdui-dialog" id="delete-swiper-' + chip_id + '"><div class="mdui-dialog-title">确定删除？</div><div class="mdui-dialog-content">删除后将无法还原！</div><div class="mdui-dialog-actions"><button class="mdui-btn mdui-ripple" mdui-dialog-close>cancel</button><button class="mdui-btn mdui-ripple mdui-color-black delete-erase" mdui-dialog-confirm>erase</button></div></div></div></div></div>';
        $swiper_panel.html($swiper_panel.html() + new_chip);
        return false;
    })

    let mao = window.location.hash;
    switch (mao) {
        case '#tab1':
            $groupFab.addClass('mdui-fab-hide');
            $newsFab.addClass('mdui-fab-hide');
            $prosFab.addClass('mdui-fab-hide');
            $indexFab.removeClass('mdui-fab-hide');
            break;
        case '#tab2':
            $groupFab.addClass('mdui-fab-hide');
            $newsFab.addClass('mdui-fab-hide');
            $prosFab.removeClass('mdui-fab-hide');
            $indexFab.addClass('mdui-fab-hide');
            break;
        case '#tab3':
            $groupFab.removeClass('mdui-fab-hide');
            $newsFab.addClass('mdui-fab-hide');
            $prosFab.addClass('mdui-fab-hide');
            $indexFab.addClass('mdui-fab-hide');
            break;
        case '#tab4':
            $groupFab.addClass('mdui-fab-hide');
            $newsFab.removeClass('mdui-fab-hide');
            $prosFab.addClass('mdui-fab-hide');
            $indexFab.addClass('mdui-fab-hide');
            break;
        case '#tab5':
            $groupFab.addClass('mdui-fab-hide');
            $newsFab.addClass('mdui-fab-hide');
            $prosFab.addClass('mdui-fab-hide');
            $indexFab.addClass('mdui-fab-hide');
            break;
    }
})

